<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <div class="area-header">
          <span class="area-header-title">{{ $t('标的公司') }}</span>
        </div>
        <el-col v-if="hideComponent" :span="24">
          <el-form-item :label="$t('基金名称')" prop="dealId">
            <DealSelect
              v-model="form.dealId"
              :disabled="form.id"
              :placeholder="$t('基金名称')"
              scope-authority="true"
              deal-type="bf"
              @change="getBfReverseInvestment"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司名称')" prop="company.companyName">
            <el-autocomplete
              v-model="form.company.companyName"
              class="inline-input name"
              :fetch-suggestions="companyQuerySearch"
              :placeholder="$t('公司名称')"
              :trigger-on-focus="false"
              :maxlength="100"
              @select="companyHandleSelect"
              @change="companyHandleSelect"
            >
              <template v-if="enableTyc" slot="append">
                <a @click="companyHandleSelect">{{ $t('查询工商信息') }}</a>
              </template>
            </el-autocomplete>
            <!-- <el-input v-else v-model="form.company.companyName" readonly show-word-limit maxlength="100">
              <template slot="append">
                <a @click="companyHandleSelect">{{ $t('查询工商信息') }}</a>
              </template>
            </el-input> -->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司简称')" prop="company.companyShortName">
            <el-input v-model="form.company.companyShortName" :placeholder="$t('公司简称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('上市情况')" prop="listingSituation">
            <dict-select
              v-model="form.company.listedSituation"
              dictionary="bf_group_listed_situation"
            />
          </el-form-item>
        </el-col>
        <el-col v-if="form.company.listedSituation !== 'no'" :span="12">
          <el-form-item :label="$t('上市/过会/申报时间')" prop="listedTime">
            <DatePicker v-model="form.company.listedTime" :placeholder="$t('预计上市时间')" />
          </el-form-item>
        </el-col>
        <el-col v-if="form.company.listedSituation !== 'no'" :span="12">
          <el-form-item :label="$t('上市板块')" prop="listedSector">
            <dict-select
              v-model="form.company.listedSector"
              dictionary="bf_group_listed_sector"
            />
          </el-form-item>
        </el-col>
        <el-col v-if="form.company.listedSituation !== 'no'" :span="12">
          <el-form-item :label="$t('股票币种')" prop="stockCurrency">
            <el-select v-model="form.company.stockCurrency" filterable :placeholder="$t('股票币种')">
              <el-option
                v-for="dict in currencyOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.company.listedSituation !== 'no'" :label="$t('股票代码')" prop="stockCode">
            <el-input v-model="form.company.stockCode" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="form.company.listedSituation !== 'no'" :label="$t('预计上市时间')" prop="expectedTimeToMarket">
            <DatePicker v-model="form.company.expectedTimeToMarket" :placeholder="$t('预计上市时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
        <div style="margin-top:15px;">
          <el-col :span="24">
            <div class="area-header">
              <span class="area-header-title">{{ $t('投资信息') }}</span>
            </div>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('投资时间')" prop="investmentTime">
              <DatePicker v-model="form.investmentTime" :placeholder="$t('投资时间')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('投资轮次')">
              <el-select v-model="form.investmentRounds" filterable :placeholder="$t('投资轮次')">
                <el-option
                  v-for="dict in investmentRoundsOptions"
                  :key="dict.dictValue"
                  :label="$t(dict.dictLabel)"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('企业阶段')">
              <el-select v-model="form.enterpriseStage" filterable :placeholder="$t('企业阶段')">
                <el-option
                  v-for="dict in enterpriseStageOptions"
                  :key="dict.dictValue"
                  :label="$t(dict.dictLabel)"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('是否符合返投标准')" prop="fitForReturn">
              <el-select v-model="form.fitForReturn" filterable :placeholder="$t('是否符合返投标准')">
                <el-option
                  v-for="dict in yesOrNoOptions"
                  :key="dict.dictValue"
                  :label="$t(dict.dictLabel)"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('返投倍数')" prop="returnMultiple">
              <Money v-model="form.returnMultiple" :placeholder="$t('返投倍数')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('投前公司估值') + $t('金额单位')" prop="preInvestmentVal">
              <Money v-model="form.preInvestmentVal" unit="w" :placeholder="$t('投前公司估值')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('投资时公司估值') + $t('金额单位')" prop="postInvestmentVal">
              <Money v-model="form.postInvestmentVal" unit="w" :placeholder="$t('投资时公司估值/投后公司估值')" />
            </el-form-item>
          </el-col>
          <el-col v-if="!form.id" :span="12">
            <el-form-item :label="$t('公司最新估值') + $t('金额单位')" prop="latestValuationOfTheCompany">
              <Money v-model="form.latestValuationOfTheCompany" unit="w" :placeholder="$t('公司最新估值')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.id === undefined" :label="$t('当前项目估值') + $t('金额单位')" prop="bfPortfolioVo.valuationResult">
              <Money v-model="form.bfPortfolioVo.valuationResult" unit="w" :placeholder="$t('当前项目估值)')" />
            </el-form-item>
          </el-col>
          <!--<el-col :span="12">-->
          <!--  <el-form-item v-if="form.id === undefined" :label="$t('估值方法')" prop="bfPortfolioVo.equityValMode">-->
          <!--    <el-select v-model="form.bfPortfolioVo.equityValMode" value="" :placeholder="$t('估值方法')">-->
          <!--      <el-option-->
          <!--        v-for="dict in equityValModeOptions"-->
          <!--        :key="dict.dictValue"-->
          <!--        :label="$t(dict.dictLabel)"-->
          <!--        :value="dict.dictValue"-->
          <!--      />-->
          <!--    </el-select>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->
          <el-col :span="12">
            <el-form-item :label="$t('协议金额') + $t('金额单位')" prop="agreementAmount">
              <Money v-model="form.agreementAmount" unit="w" :placeholder="$t('协议金额')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('子基金持股数(股)')" prop="sonFundShares" @blur="calPerShareInvestCost">
              <Money v-model="form.sonFundShares" :placeholder="$t('子基金持股数(股)')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('最新持股比例(%)')" prop="latestShareholdingRatio">
              <Ratio v-model="form.latestShareholdingRatio" :placeholder="$t('最新持股比例(%)')" :controls="false" :min="0" :max="100" :precision="2" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('IRR')" prop="irr">
              <Money v-model="form.irr" :placeholder="$t('IRR')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('划款金额') + $t('金额单位')" prop="transferAmount">
              <Money v-model="form.transferAmount" unit="w" :placeholder="$t('划款金额')" disabled @change="computationReverseInvestmentStandard" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('每股投资成本(元)')" prop="perShareInvestCost">
              <Money v-model="form.perShareInvestCost" :placeholder="$t('每股投资成本')" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="isReverseInvestment === 'Y'" :label="$t('是否符合返投标准')">
              <el-radio-group v-model="form.isCompliantReverseInvestmentStandard">
                <el-radio
                  v-for="dict in isCompliantReverseInvestmentStandard"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{ $t(dict.dictLabel) }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="isReverseInvestment === 'Y' && form.isCompliantReverseInvestmentStandard === 'Y'" :label="$t('返投倍数')" prop="reverseInvestmentMultiple">
              <Ratio v-model="form.reverseInvestmentMultiple" :min="0" :max="100" :precision="2" @change="computationReverseInvestmentStandard" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="isReverseInvestment === 'Y' && form.isCompliantReverseInvestmentStandard === 'Y'" :label="$t('返投金额') + $t('金额单位')" prop="reverseInvestmentAmount">
              <Money v-model="form.reverseInvestmentAmount" unit="w" :placeholder="$t('返投金额')" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.id !== undefined" :label="$t('已实现价值') + $t('金额单位')" prop="bfPortfolioVo.amountRecovered">
              <Money v-model="form.bfPortfolioVo.amountRecovered" unit="w" :placeholder="$t('已实现价值')" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.id !== undefined" :label="$t('已处置成本') + $t('金额单位')" prop="bfPortfolioVo.exitCost">
              <Money v-model="form.bfPortfolioVo.exitCost" unit="w" :placeholder="$t('已处置成本')" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.id !== undefined" :label="$t('已实现收益') + $t('金额单位')" prop="bfPortfolioVo.realizedRevenue">
              <Money v-model="form.bfPortfolioVo.realizedRevenue" unit="w" :placeholder="$t('已实现收益')" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.id !== undefined" :label="$t('账面成本') + $t('金额单位')" prop="bfPortfolioVo.costNotWithdrawn">
              <Money v-model="form.bfPortfolioVo.costNotWithdrawn" unit="w" :placeholder="$t('账面成本')" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.id !== undefined" :label="$t('项目最新估值') + $t('金额单位')" prop="bfPortfolioVo.latestValuation">
              <Money v-model="form.bfPortfolioVo.latestValuation" unit="w" :placeholder="$t('项目最新估值')" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.id !== undefined" :label="$t('公司整体最新估值') + $t('金额单位')" prop="bfPortfolioVo.companyWholeVal">
              <Money v-model="form.bfPortfolioVo.companyWholeVal" unit="w" :placeholder="$t('公司整体最新估值')" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.id !== undefined" :label="$t('未实现收益/损失') + $t('金额单位')" prop="bfPortfolioVo.loss">
              <Money v-model="form.bfPortfolioVo.loss" unit="w" :placeholder="$t('未实现收益/损失')" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('业务介绍')" prop="introduce">
              <el-input v-model="form.introduce" type="textarea" :placeholder="$t('业务介绍')" show-word-limit maxlength="2000" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('公司主要发展和近期计划')" prop="developmentAndPlanning">
              <el-input v-model="form.developmentAndPlanning" type="textarea" :placeholder="$t('公司主要发展和近期计划')" show-word-limit maxlength="2000" />
            </el-form-item>
          </el-col>
          <!--<el-col :span="12">-->
          <!--  <el-form-item v-if="form.id !== undefined" :label="$t('估值方法')">-->
          <!--    <el-select v-model="form.bfPortfolioVo.equityValMode" value="" :placeholder="$t('估值方法')" disabled>-->
          <!--      <el-option-->
          <!--        v-for="dict in equityValModeOptions"-->
          <!--        :key="dict.dictValue"-->
          <!--        :label="$t(dict.dictLabel)"-->
          <!--        :value="dict.dictValue"-->
          <!--        disabled-->
          <!--      />-->
          <!--    </el-select>-->
          <!--  </el-form-item>-->
          <!--</el-col>-->

          <!--          <el-col :span="12">-->
          <!--            <el-form-item v-if="form.id === undefined" :label="$t('项目估值结果') + $t('金额单位')" prop="valuationResult">-->
          <!--              <Money v-model="form.bfPortfolioVo.valuationResult" unit="w" :placeholder="$t('项目估值结果')" />-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->

        </div>
      </el-row>

      <!--合同关键条款-->
      <div style="margin-top:15px;">
        <div class="area-header">
          <span class="area-header-title">{{ $t('划款信息') }}</span>
        </div>
        <el-table
          :data="form.bfInvestGroupInfoList"
          border
          class="nowrapTable formTable tree_el_Table"
          :summary-method="getTransferAmounts"
          show-summary
        >
          <el-table-column :label="$t('序号')" align="center" width="50">
            <template slot-scope="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('出资时间')" align="center">
            <template slot="header" slot-scope="{}">
              <span>{{ $t('出资时间') }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="'bfInvestGroupInfoList.' + scope.$index + '.contributionTime'" :rules="bfInvestGroupInfoListRules.contributionTime">
                <DatePicker v-model="scope.row.contributionTime" :placeholder="$t('出资时间')" />
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column :label="$t('划款金额') + $t('金额单位')" align="center">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949;margin-right: 4px;">*</i>
              <span>{{ $t('划款金额') }}{{ $t('金额单位') }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="'bfInvestGroupInfoList.' + scope.$index + '.transferAmount'" :rules="bfInvestGroupInfoListRules.transferAmount">
                <Money v-model="scope.row.transferAmount" unit="w" />
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column :label="$t('出资占股')" align="center">
            <template slot="header" slot-scope="{}">
              <span>{{ $t('出资占股') }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item :prop="'bfInvestGroupInfoList.' + scope.$index + '.contributionShare'">
                <Money v-model="scope.row.contributionShare" :placeholder="$t('出资占股')" />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :label="$t('操作')" align="center" width="60px">
            <template slot-scope="scope">
              <el-button v-hasPermi="['bf:investGroup:remove']" type="text" icon="el-icon-delete" @click="delHandleTerms(scope.$index, scope.row)" />
            </template>
          </el-table-column>
        </el-table>
        <div class="tol">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="addBfInvestGroupInfo">{{ $t("添加") }}</el-button>
        </div>
      </div>
      <!-- 间投项目标签 -->
      <div style="margin-top: 20px;">
        <div class="area-header">
          <span class="area-header-title">{{ $t('分类标签') }}</span>
          <el-link type="primary" :underline="false" @click="tagOpen = true"><i class="el-icon-circle-plus" /></el-link>
        </div>
        <div v-if="form.tagsList !== undefined && form.tagsList !== null && form.tagsList.length > 0">
          <el-tag v-for="(item, index) in form.tagsList" :key="index" type="primary" style="margin: 5px;">
            <span v-if="item.id !== undefined && item.id !== null && item.id !== ''">{{ tagFormat(item) }}</span>
            <span v-else>{{ tagFormat(item) }}</span>
          </el-tag>
        </div>
      </div>
      <FileTable :list.sync="form.fileList" data-type="bf_invest_group" business-type="bf" :business-id="$route.query.id === undefined ? form.id : $route.query.id" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="bf:investGroup" />
    </div>

    <el-dialog
      v-if="tagOpen"
      :title="$t('标签选择')"
      :visible.sync="tagOpen"
      :close-on-click-modal="false"
      :destroy-on-close="true"
      :lock-scroll="false"
      width="80%"
      append-to-body
      top="60px"
      class="tag-dialog"
    >
      <tag-select v-slot ref="tagSelect" :chosen-tag-data="dynamicTags" :tags-bus-data="tagsBusData" :bus-id="form.id" bus-type="indirect_investment" :other-type="'indirect_investment'" class="tag-content" @close="close" @submitTag="submitTag" />
      <div slot="footer" class="dialog-footer" style="text-align: right;">
        <!--保存 取消按钮-->
        <save-btn has-permi="deal:tags" :function="submitFormTab" :cancel-func="close" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { companyListByName } from '@/api/company/base/companyBase'
import {
  saveBfInvestGroup,
  deleteBfInvestGroupInfo,
  getBfInvestGroup,
  checkCompanyName,
  checkShortName
} from '@/api/bf/investGroup/bfInvestGroup'
import { findAll, getBfBaseIsReverseInvestment } from '@/api/bf/base/bfBase'
import TagSelect from '@/components/Tag/TagSelect'
// 行业组件
// import Industry from '@/components/Industry'
// 省市组件
// import Area from '@/components/Area'
import DealSelect from '@/components/Select/dealSelect'
import { getCompanyByName } from '@/api/company/base/companyBase'
export default {
  components: {
    // Industry,
    // Area,
    TagSelect,
    DealSelect
  },
  props: {
    hideComponent: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const checkShotName = (rule, value, callback) => {
      if (value === undefined || value === '' || value === null) {
        callback(new Error(this.$t('必填信息')))
      } else {
        if (!this.isEmpty(this.form.dealId)) {
          checkShortName(value, this.form.dealId).then(res => {
            if (res.data) {
              if (res.data.id === this.form.id) {
                callback()
              } else {
                callback(new Error(this.$t('公司简称已存在')))
              }
            } else {
              callback()
            }
          })
        } else {
          callback()
        }
      }
    }
    return {
      otherBtnHide: false,
      // 是否有返投
      isReverseInvestment: 'N',
      // 注册资本币种字典
      currencyOptions: [],
      // 是否上市
      yesOrNoOptions: [],
      // 企业阶段字典
      enterpriseStageOptions: [],
      // 投资轮次
      investmentRoundsOptions: [],
      // 股权估值方法
      equityValModeOptions: [],
      // 是否符合返投标准
      isCompliantReverseInvestmentStandard: [],
      // 表单参数
      form: {
        company: {},
        tagsList: [],
        bfPortfolioVo: {},
        bfInvestGroupInfoList: []
      },
      // 遮罩层
      loading: false,
      // 表单校验
      rules: {
        'company.companyName': [
          { required: true, message: '必填信息',	trigger: 'blur' },
          { validator: this.checkCompanyName, trigger: 'blur' }
          ],
        'company.companyShortName': [{ required: true, validator: checkShotName,	trigger: 'blur' }],
        agreementAmount: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        latestShareholdingRatio: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        dealId: { required: true, validator: checkShotName, trigger: 'blur' },
        investmentTime: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        postInvestmentVal: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        // 'bfPortfolioVo.companyWholeVal': { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        'bfPortfolioVo.valuationResult': { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        'bfPortfolioVo.equityValMode': { required: true, message: this.$t('必填信息'), trigger: 'blur' }
      },
      bfInvestGroupInfoListRules: {
        contributionShare: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
        transferAmount: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
      },
      // 天眼查查询条件
      tycParam: {
        id: '',
        companyName: '',
        tycId: ''
      },
      // 参股基金列表
      bfBaseList: [],
      // 标签选择页面
      tagOpen: false,
      // 当前选中的标签
      dynamicTags: [],
      // 标签选项赛道传值
      tagsBusData: [],
      enableTyc: undefined
    }
  },
  mounted() {
    this.$bus.$emit('async_done')
    this.enableTyc = (window.localStorage.getItem('enableTyc') === 'true')
  },
  created() {
    this.getDicts('currency').then(response => {
      this.currencyOptions = response.data
    })
    this.getDicts('equity_investment_type ').then(response => {
      this.enterpriseStageOptions = response.data
    })
    this.getDicts('rounds_investment').then(response => {
      this.investmentRoundsOptions = response.data
    })
    this.getDicts('sys_yes_no').then(response => {
      this.yesOrNoOptions = response.data
      this.isCompliantReverseInvestmentStandard = response.data
    })
    findAll().then(res => {
      this.bfBaseList = res.data
    })
    this.getDicts('valuation_method').then(response => {
      this.equityValModeOptions = response.data
    })
  },
  methods: {
    // 初始化数据
    init(data, isReverseInvestment) {
      this.loading = true
      this.isReverseInvestment = isReverseInvestment
      if (data != null && data.id !== undefined) {
        getBfInvestGroup(data.id).then(response => {
          this.form = response.data
          if (this.$route.query.source === 'BF_PORTFOLIO') {
            this.getBfReverseInvestment(response.data.dealId)
          }
          this.$nextTick(() => {
            this.$refs.industryZj.initIndustrySecond()
            this.$refs.cityZj.initCity()
            this.$refs.cityZj.initDistrict()
          })
          this.dynamicTags = (this.form.tagsList === null ? [] : this.form.tagsList)
          this.loading = false
        })
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        company: {
          industryFirst: undefined,
          industrySecond: undefined,
          provinceRegistered: undefined,
          cityRegistered: undefined
        },
        bfPortfolioVo: {},
        id: undefined,
        dealId: this.$route.query.id,
        indirectDealId: undefined,
        investmentTime: undefined,
        investmentRounds: undefined,
        enterpriseStage: undefined,
        fitForReturn: undefined,
        returnMultiple: undefined,
        preInvestmentVal: undefined,
        postInvestmentVal: undefined,
        latestValuationOfTheCompany: undefined,
        agreementAmount: undefined,
        sonFundShares: undefined,
        transferAmount: undefined,
        perShareInvestCost: undefined,
        focusIndustry: undefined,
        latestShareholdingRatio: undefined,
        irr: undefined,
        isCompliantReverseInvestmentStandard: 'N',
        reverseInvestmentMultiple: undefined,
        reverseInvestmentAmount: undefined,
        remark: undefined,
        developmentAndPlanning: undefined,
        introduce: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        logoInfo: undefined,
        delFlag: undefined,
        bfInvestGroupInfoList: undefined,
        companyId: undefined,
        name: undefined,
        investTime: undefined,
        tagsList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      this.$parent.$parent.formOpen = false
      // 刷新list页面
      this.$parent.$parent.getList()
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveBfInvestGroup(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        } else {
          this.locationError(document)
        }
      })
    },
    // 公司全称校验是否重复的校验方法
    async checkCompanyName(rule, value, callback) {
      var checkRes = await checkCompanyName(this.form.company.companyName, this.form.dealId, this.form.indirectDealId)
      if (checkRes) {
        callback()
      } else {
        callback(new Error(this.$t('该公司已存在，不能重复添加')))
      }
    },
    // 计算报销总额
    getTransferAmounts() {
      let sum = 0
      if (this.form.bfInvestGroupInfoList !== null && this.form.bfInvestGroupInfoList !== undefined && this.form.bfInvestGroupInfoList.length > 0) {
        this.form.bfInvestGroupInfoList.forEach(item => {
          if (item.transferAmount !== undefined && item.transferAmount !== null && item.transferAmount !== '') {
            sum += +item.transferAmount
          }
        })
        this.$set(this.form, 'transferAmount', sum)
      } else {
        this.form.transferAmount = undefined
      }
      this.calPerShareInvestCost()
      this.computationReverseInvestmentStandard()
      return sum
    },
    // 计算每股投资成本
    calPerShareInvestCost() {
      if (this.form.transferAmount !== undefined && this.form.transferAmount !== null && this.form.transferAmount !== '') {
        if (this.form.sonFundShares !== undefined && this.form.sonFundShares !== null && this.form.sonFundShares !== '') {
          console.log('this.form.sonFundShares', this.form.sonFundShares)
          console.log('this.form.transferAmount', this.form.transferAmount)
          var count = this.form.transferAmount / this.form.sonFundShares
          this.$set(this.form, 'perShareInvestCost', count)
          console.log('this.form.perShareInvestCost', this.form.perShareInvestCost)
        }
      }
    },
    // 标的公司查询
    companyQuerySearch(queryString, cb) {
      companyListByName(queryString).then(res => {
              if (res.code === 200) {
                cb(res.data)
              }
      })
    },
    // 标的公司选择后事件
    companyHandleSelect(item) {
      var companyName = this.form.company.companyName
      if (companyName === '' || companyName === undefined) {
        this.msgError(this.$t(this.$t('请先输入公司全称！')))
        return
      }
      this.tycParam.id = this.form.id
      this.tycParam.tycId = item.id
      this.tycParam.companyName = companyName
      this.loading = true
      getCompanyByName(this.tycParam).then(res => {
        if (res.msg === 'exist') {
          this.form.companyName = ''
          this.msgError(this.$t('公司已存在,请重新录入！'))
        } else if (res.msg === 'nofind') {
          this.form.companyName = ''
          this.msgError(this.$t('天眼查没有获取到该公司信息！'))
        } else {
          // this.formCleaing()
          const tempConpanyId = this.form.company.id
          this.form.company = res.data
          if (tempConpanyId) {
               this.form.company.id = tempConpanyId
          }
          // 公司简称
          this.form.company.companyShortName = companyName
          // 公司logo
          this.form.company.logoInfo = res.data.logo
          this.$nextTick(() => {
            if (this.$refs.industryZj?.initIndustrySecond !== undefined) {
              this.$refs.industryZj.initIndustrySecond()
            }
          })
          this.$nextTick(() => {
            if (this.$refs.cityZj?.initCity !== undefined) {
              this.$refs.cityZj.initCity()
            }
            if (this.$refs.cityZj?.initDistrict !== undefined) {
              this.$refs.cityZj.initDistrict()
            }
          })

          // 关联公司ID
          this.form.indirectDealId = res.data.id
          this.msgSuccess(this.$t('已获取工商数据！'))
        }
        this.loading = false
      })
    },
    // 添加投资组合信息
    addBfInvestGroupInfo() {
      const row = {
        id: undefined,
        contributionTime: '',
        transferAmount: '',
        contributionShare: '',
        remark: ''
      }
      if (!(this.form.bfInvestGroupInfoList instanceof Array)) {
        this.$set(this.form, 'bfInvestGroupInfoList', [])
      }
      this.$nextTick(() => {
        this.form.bfInvestGroupInfoList.push(row)
      })
    },
    // 删除关键条款
    delHandleTerms(index, row) {
      // eslint-disable-next-line no-undef
      const id = row.id
      if (id !== undefined && id !== '' && id != null) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          type: 'warning'
        })
          .then(function() {
            return deleteBfInvestGroupInfo(id)
          }).then(() => {
          this.form.bfInvestGroupInfoList.splice(index, 1)
          return this.msgSuccess(this.$t('删除成功'))
        })
          .catch(function() {})
      } else {
        this.form.bfInvestGroupInfoList.splice(index, 1)
      }
      this.getTransferAmounts()
    },
    submitFormTab() {
      this.$refs.tagSelect.submitFormOut()
    },
    submitTag(data) {
      console.log('标签传值data', data)
      this.tagOpen = false
      if (this.form.tagsList === null) {
        this.form.tagsList = []
      }
      this.form.tagsList = data.formArray
      this.dynamicTags = data.formArray
      this.tagsBusData = data.tagsBusData
    },
    tagFormat(item) {
      let result = item.configureLabel
      if (item.firstEdit === 'Y' && item.tagContent) {
        result += ' / ' + item.tagContent
      } else {
        if (item.firstTagLabel) {
          result += ' / ' + item.firstTagLabel
        }
        if (item.secondTagLabel) {
          result += ' / ' + item.secondTagLabel
        }
        if (item.thirdTagLabel) {
          result += ' / ' + item.thirdTagLabel
        }
        if (item.fourthTagLabel) {
          result += ' / ' + item.fourthTagLabel
        }
        if (item.fifthTagLabel) {
          result += ' / ' + item.fifthTagLabel
        }
      }
      return result
    },
    close() {
      this.tagOpen = false
    },
    // 计算返投金额
    computationReverseInvestmentStandard() {
      if (this.form.transferAmount !== undefined && this.form.reverseInvestmentMultiple !== undefined && this.form.transferAmount !== '' && this.form.reverseInvestmentMultiple !== '') {
        const transferAmount = this.form.transferAmount ? Number(this.form.transferAmount) : 0
        const reverseInvestmentMultiple = this.form.reverseInvestmentMultiple ? Number(this.form.reverseInvestmentMultiple) : 0
        this.form.reverseInvestmentAmount = transferAmount * reverseInvestmentMultiple
      } else {
        this.form.reverseInvestmentAmount = undefined
      }
    },
    getBfReverseInvestment(dealId) {
      if (dealId) {
        // 查询基金是否有返投
        getBfBaseIsReverseInvestment(dealId).then(res => {
          this.isReverseInvestment = res.msg
        })
      }
    }
  }

}
</script>

<style lang="scss" scoped>
.tianyan{
  border: 1px solid #DCDFE6;
  width: 17%;
  border-radius: 5px;
  height: 36px;
  float: right;
  text-align: right;
  padding-right: 15px;
  border-left: 0px;
  color: #1E9FFF;
  margin-top: -37px;
  font-size: 13px;
}
.tag-dialog /deep/ .el-dialog__body {
  height: 100%;
  overflow: auto;
}
.tag-dialog .tag-content {
  height: 600px;
}
</style>
